package com.best.oasis.settlement.dao.system;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.best.oasis.settlement.common.configmapping.EntitySequenceMapping;
import com.best.oasis.settlement.common.entity.system.MenuItem;
import com.best.oasis.settlement.dao.base.BaseDaoImpl;

@Repository
public class MenuItemDaoImpl extends BaseDaoImpl<MenuItem, Long> implements MenuItemDao {

    public MenuItemDaoImpl(){
        super(MenuItem.class);
    }
    
	/**
	 * 获取ROOT节点
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public MenuItem  getRootMenu(){
		List<MenuItem>  list  = (List<MenuItem>) sqlSessionTemplate.selectList(getStatementPrefix()+"getRootMenu");
        return (MenuItem) list.get(0);
	}
	
	  /**
     * 获取菜单列表
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<MenuItem> getMenuItemList(Long userId) {
        List<MenuItem>  list  = (List<MenuItem>) sqlSessionTemplate.selectList(getStatementPrefix()+"getMenuItemList",userId);
        return list;
    }

    @Override
   	public Long getNextSequence() {
       	String sequenceName = EntitySequenceMapping.MAP.get(this.entityClass);
       	if(StringUtils.isBlank(sequenceName)){
       		sequenceName = EntitySequenceMapping.DEFAULT_ID_SEQUENCE;
       	}
       	return nextval(sequenceName);
   	}
    
    // 获取指定序列的下一个值
 	private Long nextval(String sequenceName) {
 		Object nextval = sqlSessionTemplate.selectOne("sqlmap.common.getIdSequence",sequenceName);
 		
 		if(nextval!=null){
 			return ((BigDecimal)nextval).longValue();	
 		}
 		
 		return null;
 	}
	
}
